package com.wyh.utils.sqlUtils.user;

import com.wyh.entity.Account;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

public class addUserSqlDynamic {
    public String addDynamic(@NotNull Account user) {
        StringBuilder builder = new StringBuilder();
        StringBuilder parameterPlaceholders = new StringBuilder();
        List<String> parameters = new ArrayList<>();

        if (user.getName() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("name");
            parameterPlaceholders.append("#{name}");
            parameters.add("name");
        }
        if (user.getEmail() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("email");
            parameterPlaceholders.append("#{email}");
            parameters.add("email");
        }
        if (user.getTelephoneNumber() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("telephoneNumber");
            parameterPlaceholders.append("#{telephoneNumber}");
            parameters.add("telephoneNumber");
        }
        if (user.getCN_ID() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("CN_ID");
            parameterPlaceholders.append("#{CN_ID}");
            parameters.add("CN_ID");
        }
        if (user.getPermission() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("permission");
            parameterPlaceholders.append("#{permission}");
            parameters.add("permission");
        }
        if (user.getGender() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("gender");
            parameterPlaceholders.append("#{gender}");
            parameters.add("gender");
        }
        if (user.getRemark() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("remark");
            parameterPlaceholders.append("#{remark}");
            parameters.add("remark");
        }
        if (user.getUsername() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("username");
            parameterPlaceholders.append("#{username}");
            parameters.add("username");
        }
        if (user.getPassword() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("password");
            parameterPlaceholders.append("#{password}");
            parameters.add("password");
        }

        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO user (");
        sql.append(builder);
        sql.append(") VALUES (");
        sql.append(parameterPlaceholders);
        sql.append(");");

        System.out.println(sql);
        return sql.toString();
    }

}